<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button type="primary" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['orders:orders:export']">下载报表</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['orders:orders:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['orders:orders:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['orders:orders:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="number" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{scope.row.status == 1 ? '待付款' : scope.row.status == 2 ? '待派送' : scope.row.status == 3 ? '已派送' : scope.row.status == 4 ? '已完成' : '已取消'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实收金额" align="center" prop="amount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['orders:orders:edit']"
          >查看</el-button>
          <el-button size="mini" type="text" @click="quxiao(scope.row)">
              <span>{{scope.row.status == 1 ? '取消' : scope.row.status == 2 ? '派送' : scope.row.status == 3 ? '完成' : scope.row.status == 4 ? '' : '取消'}}</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" >
        <el-form-item label="订单号" prop="number">
          <el-input v-model="form.number" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="下单用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入下单用户" />
        </el-form-item>
        <el-form-item label="地址id" prop="addressBookId">
          <el-input v-model="form.addressBookId" placeholder="请输入地址id" />
        </el-form-item>
        <el-form-item label="下单时间" prop="orderTime">
          <el-date-picker clearable
            v-model="form.orderTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结账时间" prop="checkoutTime">
          <el-date-picker clearable
            v-model="form.checkoutTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结账时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式" prop="payMethod">
          <el-input v-model="form.payMethod" placeholder="请输入支付方式 1微信,2支付宝" />
        </el-form-item>
        <el-form-item label="实收金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入实收金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入收货人电话" />
        </el-form-item>
        <el-form-item label="收货人地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入收货人地址" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="收货人名字" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货人名字" />
        </el-form-item>
        <el-divider content-position="center">订单明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddOrderDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteOrderDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="orderDetailList" :row-class-name="rowOrderDetailIndex" @selection-change="handleOrderDetailSelectionChange" ref="orderDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="名字" prop="name" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入名字" />
            </template>
          </el-table-column>
          <el-table-column label="菜品id" prop="dishId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dishId" placeholder="请输入菜品id" />
            </template>
          </el-table-column>
          <el-table-column label="套餐id" prop="setmealId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.setmealId" placeholder="请输入套餐id" />
            </template>
          </el-table-column>
          <el-table-column label="口味" prop="dishFlavor" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dishFlavor" placeholder="请输入口味" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="number" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.number" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="金额" prop="amount" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.amount" placeholder="请输入金额" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    &lt;!&ndash; 查看订单对话框 -->
    <el-dialog :title="title1" :visible.sync="open1" width="700px" append-to-body>
      <el-form :inline="true" ref="form" :model="form">
        <el-form-item label="订单号" label-width="80px">
          <el-input v-model="form.number" style="width: 230px" readonly/>
        </el-form-item>
        <el-form-item label="订单状态" label-width="80px">
          <el-input v-model="form.status == 1 ? '待付款' : form.status == 2 ? '待派送' : form.status == 3 ? '已派送' : form.status == 4 ? '已完成' : '已取消'" style="width: 230px" readonly />
        </el-form-item>
        <el-form-item :inline="true" label="收货人" label-width="80px">
          <el-input v-model="form.consignee" style="width: 230px" readonly/>
        </el-form-item>
        <el-form-item :inline="true" label="联系电话" label-width="80px">
          <el-input v-model="form.phone" style="width: 230px" readonly/>
        </el-form-item>
        <el-form-item label="地址" :inline="true" label-width="80px">
          <el-input v-model="form.address" style="width: 230px" readonly/>
        </el-form-item>
        <el-form-item label="支付金额" label-width="80px" :inline="true">
          <el-input v-model="form.amount" style="width: 230px" readonly/>
        </el-form-item>
        <el-form-item label="下单时间" label-width="80px">
          <el-date-picker v-model="form.orderTime" disabled>
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel1">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrders, getOrders, delOrders, addOrders, updateOrders, updateStatus} from "@/api/orders/orders";

export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedOrderDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      ordersList: [],
      // 订单明细表格数据
      orderDetailList: [],
      // 弹出层标题
      title: "",
      title1: "查看订单",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        number: null,
        status: null,
        userId: null,
        addressBookId: null,
        orderTime: null,
        checkoutTime: null,
        payMethod: null,
        amount: null,
        phone: null,
        address: null,
        userName: null,
        consignee: null
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "订单状态 1待付款，2待派送，3已派送，4已完成，5已取消不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "下单用户不能为空", trigger: "blur" }
        ],
        addressBookId: [
          { required: true, message: "地址id不能为空", trigger: "blur" }
        ],
        orderTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ],
        checkoutTime: [
          { required: true, message: "结账时间不能为空", trigger: "blur" }
        ],
        payMethod: [
          { required: true, message: "支付方式 1微信,2支付宝不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "实收金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        console.log(response);
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;

    },
    quxiao(row){
      if (row.status==1){
        row.status=5;
        updateStatus(row).then(response=>{
          console.log(response);
          this.getList();
        })
      }else if(row.status==2){
        row.status=3;
        updateStatus(row).then(response=>{
          console.log(response);
          this.getList();
        })
      }else if(row.status==3){
        row.status=4;
        updateStatus(row).then(response=>{
          console.log(response);
          this.getList();
        })
      }else{
        row.status=1;
        updateStatus(row).then(response=>{
          console.log(response);
          this.getList();
        })
      }

    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        number: null,
        status: 0,
        userId: null,
        addressBookId: null,
        orderTime: null,
        checkoutTime: null,
        payMethod: null,
        amount: null,
        remark: null,
        phone: null,
        address: null,
        userName: null,
        consignee: null
      };
      this.orderDetailList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    DownloadQuery() {

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data;
        this.orderDetailList = response.data.orderDetailList;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 修改按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data;
        this.orderDetailList = response.data.orderDetailList;
        this.open1 = true;
        this.title = "查看订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.orderDetailList = this.orderDetailList;
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
   handleCk(){

   },
	/** 订单明细序号 */
    rowOrderDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单明细添加按钮操作 */
    handleAddOrderDetail() {
      let obj = {};
      obj.name = "";
      obj.image = "";
      obj.dishId = "";
      obj.setmealId = "";
      obj.dishFlavor = "";
      obj.number = "";
      obj.amount = "";
      this.orderDetailList.push(obj);
    },
    /** 订单明细删除按钮操作 */
    handleDeleteOrderDetail() {
      if (this.checkedOrderDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的订单明细数据");
      } else {
        const orderDetailList = this.orderDetailList;
        const checkedOrderDetail = this.checkedOrderDetail;
        this.orderDetailList = orderDetailList.filter(function(item) {
          return checkedOrderDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleOrderDetailSelectionChange(selection) {
      this.checkedOrderDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('orders/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
